<?php
/**
 * Created by PhpStorm.
 * User: wjj
 * Date: 2016/11/1
 */

namespace app\logic;


use app\models\Channel;
use yii;


class ChannelLogic
{
    public static $input_type_list = ['下拉框','输入框','单选项','多选项'];

    public function  getChannels(){
        return Channel::find()->asArray()->all();
    }

    public function getChannel($query = [], $page = 1, $limit = 10)
    {
        $channel = Channel::find();
        if(isset($query['channel_name'])){
            $channel->filterWhere(['LIKE','channel_name',$query['channel_name']]);
        }
        if(isset($query['channel_code'])){
            $channel->filterWhere(['LIKE','channel_code',$query['channel_code']]);
        }
        $count = $channel->count();
        $channel->orderBy("channel_id desc")
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $channel->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $count,
                'page' => $page,
                'limit' => $limit,
                'url' => 'channel/index',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function addChannel($params)
    {
        $channel = new Channel();
        $channel-> channel_name = $params['channel_name'];
        $channel-> channel_code = $params['channel_code'];
        $channel-> channel_link = Yii::$app->params['domain']['all_domain']."?channel=".$params['channel_code'];
        $channel->save();
        return true;
    }

    public function getById($id)
    {
        return Channel::find()->where(['channel_id' => $id])->asArray()->one();
    }

    public function deleteById($id)
    {
        $channel = Channel::findOne($id);
        return $channel->delete();
    }

}
